/**
 * Created by hjx on 2018/2/7.
 */

(function () {
    var exports = this;
    var $ = null;
    var clubsInfo = [];
    var selectClubId = 0;
    var msgListeners = {};

    var javaClubMainLayer = cc.Layer.extendEX({
        _joinClubLayer:null,
        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
            network.removeListenersForTarget(this);
        },
        ctor: function () {
            this._super();
            
            this.loadCCS();
            this.initClickListener();
            this._initNetworkListeners();
            this.handleMsg();

            return true;
        },
        /*
        解析JSON资源
         */
        loadCCS:function () {
            loadNodeCCS(res.javaClubMainLayer_json, this);
            $ = create$(this.getChildByName('Layer'));
            //创建亲友圈功能 暂时不开启
            $('btn_create').setVisible(true);
        },
        /*
         加载获取俱乐部列表监听
         */
        _initNetworkListeners: function () {
            this.addListener(clubMsg.getClubList, this.refrenshTableView.bind(this));
            this.addListener(clubMsg.createClub, this.createClubSuccess.bind(this));
            this.addListener(clubMsg.joinClub, this.joinClubCallBack.bind(this));

        },
        /*
         加载获取俱乐部列表按钮监听
         */
        initClickListener:function () {
            var that = this ;
            TouchUtil.setOnclickListener($('root'), function () {
            });

            //btn_join
            TouchUtil.setOnclickListener($('btn_join'), function () {
                that._joinClubLayer = new javaClubInputLayer('join')
                that.addChild(that._joinClubLayer );
            });

            TouchUtil.setOnclickListener($('btn_message'), function () {
                that.addChild(new ClubInviteMessageLayer());
            });

            TouchUtil.setOnclickListener($('btn_help'), function () {
                that.addChild(new javaGuideLayer());
            });

            TouchUtil.setOnclickListener($('btn_refresh'), function () {

            });

            TouchUtil.setOnclickListener($('btn_create'), function () {
                that.addChild(new javaClubCommonInputLayer('请输入亲友圈名称', that.createClubHandler.bind(that)));
            });

            TouchUtil.setOnclickListener($('btn_close'), function () {
                that.getParent().removeFromParent(true);
            });
        },
        handleMsg:function () {
            network.send(clubMsg.getClubList,{});
            if(clubMsg.Debug){
                network.recv(clubMsg.msg_8001);
            }
        },
        addListener: function (code, listener) {
            network.addListener(code, listener, this);
        },
        //创建俱乐部 确定按钮 回调
        createClubHandler: function (inputStr) {
            if(inputStr.length <= 0){
                showAlert('俱乐部名称不能为空哦')
            }
            network.send(clubMsg.createClub, {name: inputStr});
            if(clubMsg.Debug){
                network.recv(clubMsg.msg_8002);
                network.recv(clubMsg.msg_8001);
            }
        },
        
        //消息列表
        setWeiduMegNum: function (num, node) {
            if (!$ || !node) {
                return;
            }
            $('numbg', node).setVisible(num > 0);
            $('numbg.num', node).setString(num);
            node.setVisible(num > 0);
        },

        //俱乐部显示规则:
        //上次打开的俱乐部显示在第一个位置
        clubViewDataSort: function (dataArr) {
            var clubId = cc.sys.localStorage.getItem('SelectClub') || 0;
            var index = -1;
            for (var i = 0; i < dataArr.length; i++) {
                var obj = dataArr[i];
                if (obj.id == clubId) {
                    index = i;
                }
            }
            if (index != -1) {
                var temp = dataArr[0];
                dataArr[0] = dataArr[index];
                dataArr[index] = temp;
            }
            return dataArr;
        },
        createClubSuccess: function (data) {
            showToast(data.msg);
        },
        joinClubCallBack: function (data) {
            showToast(data.msg);
            if(this._joinClubLayer){
                this._joinClubLayer.removeFromParent();
                this._joinClubLayer = null ;
            }
        },
        refrenshTableView: function (data) {
            var dataArr = data.clublist;
            if (!$ || !$('scrolView')) {
                hideLoading();
                return;
            }
            this.scrollView = $('scrolView');
            this.scrollView.removeAllChildren(true);


            this.state = 1;


            if (!dataArr || !dataArr.length || dataArr.length < 1) {
                hideLoading();
                $('meinv').setVisible(true);
                return;
            } else {
                $('meinv').setVisible(false);
            }

            dataArr = this.clubViewDataSort(dataArr);

            this.layout = new ccui.Layout();
            this.totalWidth = 0;
            for (var i = 0; i < dataArr.length; i++) {
                this.initItem(dataArr[i]);
            }
            this.scrollView.setInnerContainerSize(cc.size(this.totalWidth, this.scrollView.getContentSize().height));
            this.scrollView.addChild(this.layout);
            this.layout.setPositionX(0);
        },
        initItem: function (itemData) {
            var item = new javaClubInfoItem(itemData, this);
            this.layout.addChild(item);
            hideLoading();
            var itemWidth = item.getLayerWidth();
            item.setPositionX(this.totalWidth);
            this.totalWidth += itemWidth;
        }
    });
    /*
    exports.javaClubMainLayer = javaClubMainLayer;
    exports.getClubData = function (id) {
        if (id) {
            for (var i = 0; i < clubsInfo.length; i++) {
                if (clubsInfo[i]._id == id) {
                    return clubsInfo[i];
                }
            }
        }
        return clubsInfo;
    };
    //是不是管理员
    exports.isAdmin = function (clubData) {
        var adminsArr = clubData['admins'];
        var isInAdmins = false;
        if(adminsArr) {
            for (var i = 0; i < adminsArr.length; i++) {
                if (gameData.uid == adminsArr[i]) {
                    isInAdmins = true;
                    break;
                }
            }
            if (gameData.uid == clubData.owner_uid) {
                isInAdmins = true;
            }
        }
        return isInAdmins;
    };
    //获取玩家信息学
    exports.getUserInClub = function (clubData, uid) {
        var members = clubData['members'];
        if(members){
            for(var i=0;i<members.length;i++){
                if(members[i]['uid'] == uid){
                    return members[i];
                }
            }
        }
        return null;
    };
    //获取玩家体力
    exports.getUserHP = function(clubData, uid){
        if(clubData.hp_value){
            for(var i=0;i<clubData.hp_value.length;i++){
                if(clubData.hp_value[i]['uid'] == uid){
                    return clubData.hp_value[i]['hp'];
                }
            }
            return 0;
        }
        return 0;
    };
    //体力值是不是开启
    exports.isPowerOpen = function(clubid, isOwner){
        if(!gameData.opt_conf_2.openClubPower)  return false;
        if(isOwner) return true;
        var clubInfo = getClubData(clubid);
        if(clubInfo && clubInfo['hp_stats'] == 'up')  return true;
        return false;
    };
    exports.refreshClubData = function (id, data) {
        if (_.isArray(id)) {
            clubsInfo = id;
        } else {
            for (var i = 0; i < clubsInfo.length; i++) {
                if (clubsInfo[i]._id == id) {
                    clubsInfo[i] = data;
                    break;
                }
            }
        }
        return data;
    };
    exports.addClubInfoListener = function (key, callback) {
        if (!msgListeners[key]) {
            msgListeners[key] = [];
        }
        for (var i = 0; i < msgListeners[key].length; i++) {
            if (callback == msgListeners[key][i]) {
                return callback;
            }
        }
        msgListeners[key].push(callback);
        return callback;
    };
    exports.removeClubInfoListener = function (key, callback) {
        // msgListeners[key] = undefined;
        if (callback && msgListeners[key]) {
            var idx = msgListeners[key].indexOf(callback);
            if (idx >= 0) {
                msgListeners[key].splice(idx, 1);
            }
        } else {
            msgListeners[key] = undefined;
        }
    };
    exports.tvibrate = function (s) {
        if (cc.sys.os == cc.sys.OS_IOS)
            cc.Device.vibrate(s);
        else if (cc.sys.os == cc.sys.OS_ANDROID) {
            cc.Device.vibrate(s);
        }
    };
*/
})(window);